package versionx

import (
	"gitee.com/zhongguo168a/gocodes/datax"
	"strings"
)

func NewRoot(source *Source) (obj *Root) {
	obj = &Root{}
	obj.Object = &Object{}
	obj.Source = source
	obj.data = source.origin
	obj.versionPaths = append(obj.versionPaths, "")
	obj.temps = datax.M{}
	return
}

// 根
type Root struct {
	*Object
}

func (s *Root) RefOnChange(f func(path, field string, val interface{})) {
	s.onChange = f
}

// 编号
func (s *Root) RefId() string {
	return s.RefString("_id")
}

// 设置根
func (s *Root) RefIdSet(val string) {
	s.RefSet("_id", val)
}

func (s *Root) RefClone() (r *Root) {
	r = NewRoot(s.Source.Clone())
	r.data = r.origin
	return
}

func (s *Root) RefDispose() {
	s.Source.Dispose()
	s.Source = nil
	s.data = nil
}

func (s *Root) RefReset(val map[string]interface{}) {
	s.Source.RefReset(val)
	s.data = s.Source.origin
	s.temps = datax.M{}

}

func ParseKey(key string) (className, id string) {
	arr := strings.Split(key, "/")
	className = arr[0]
	id = arr[1]
	return
}
